<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <!-- 长风软件类型特有字段 -->
          <a-col :span="12" v-if="softwareType === '0'">
            <a-form-item label="窗号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入窗号" v-decorator="['windowNo']" />
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="配件编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag
                placeholder="请选择配件"
                v-decorator="['accessoryCode', { rules: [{ required: true, message: '请选择配件!' }] }]"
                dict="wms_accessory,accessory_code,id"
                @change="handleAccessoryChange"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="配件名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="选择配件后自动带出" disabled v-decorator="['accessoryName']" />
            </a-form-item>
          </a-col>

          <!-- 长风软件类型特有字段 -->
          <template v-if="softwareType === '0'">
            <a-col :span="12">
              <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input placeholder="请输入规格" v-decorator="['specification']" />
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="长度/数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input placeholder="请输入长度/数量" v-decorator="['lengthQuantity']" />
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="品牌" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input placeholder="请输入品牌" v-decorator="['brand']" />
              </a-form-item>
            </a-col>
          </template>

          <a-col :span="12">
            <a-form-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                style="width: 100%"
                placeholder="请输入数量"
                :min="1"
                v-decorator="['quantity', { rules: [{ required: true, message: '请输入数量!' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input placeholder="请输入单位" v-decorator="['unit', { initialValue: '个' }]" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { FormMixin } from '@/mixins/formMixin'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import { getAction } from '@/api/manage'

// 使用require方式导入uuid
const uuidv4 = require('uuid').v4

export default {
  name: 'AccessoryFormModal',
  mixins: [FormMixin],
  components: {
    JSearchSelectTag,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: '添加配件明细',
      visible: false,
      model: {},
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      confirmLoading: false,
      softwareType: '0', // 默认长风
      editIndex: undefined, // 编辑时的索引
      url: {
        getAccessoryInfo: '/wms/accessory/getById',
      },
    }
  },
  methods: {
    // 添加配件明细
    add(softwareType) {
      this.edit({}, softwareType)
    },

    // 编辑配件明细
    edit(record, softwareType, index) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.softwareType = softwareType
      this.editIndex = index

      // 设置标题
      this.title = index !== undefined ? '编辑配件明细' : '添加配件明细'

      // 如果是编辑，设置表单值
      if (this.model.id) {
        this.$nextTick(() => {
          this.form.setFieldsValue(this.model)
        })
      }
    },

    // 关闭弹窗
    close() {
      this.visible = false
      this.editIndex = undefined
    },

    // 提交表单
    handleOk() {
      const that = this

      // 表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true

          // 构建提交数据
          let formData = Object.assign(this.model, values)

          // 确保有ID
          if (!formData.id) {
            formData.id = uuidv4()
          }

          // 设置领料状态为未领料
          formData.status = 0

          // 发送数据到父组件
          this.$emit('ok', formData, this.editIndex)

          that.confirmLoading = false
          that.close()
        }
      })
    },

    // 选择配件后的回调
    handleAccessoryChange(value) {
      if (value) {
        // 获取配件信息
        getAction(`${this.url.getAccessoryInfo}?id=${value}`).then((res) => {
          if (res.success && res.result) {
            // 设置配件名称和其他信息
            this.form.setFieldsValue({
              accessoryName: res.result.accessoryName,
              specification: res.result.specification || '',
              unit: res.result.unit || '个',
              brand: res.result.brand || '',
            })
          }
        })
      } else {
        this.form.setFieldsValue({
          accessoryName: '',
          specification: '',
          unit: '个',
          brand: '',
        })
      }
    },
  },
}
</script>

<style scoped>
</style> 